package com.company.genealogy.web.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.company.genealogy.web.entity.FamilyNews;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 家族动态Mapper
 */
@Mapper
public interface FamilyNewsMapper extends BaseMapper<FamilyNews> {

    /**
     * 获取新闻列表
     */
    @Select("SELECT id, title, description, image, type, view_count, like_count, created_at " +
            "FROM tb_family_news " +
            "WHERE status = 1 " +
            "ORDER BY created_at DESC " +
            "LIMIT #{offset}, #{size}")
    List<Map<String, Object>> getNewsList(@Param("offset") int offset, @Param("size") int size);

    /**
     * 统计新闻总数
     */
    @Select("SELECT COUNT(*) FROM tb_family_news WHERE status = 1")
    Long countTotalNews();

    // 按家族分页获取动态
    List<Map<String, Object>> getNewsListByClanId(@Param("offset") int offset, @Param("size") int size, @Param("clanId") Long clanId);
    // 按家族统计动态总数
    Long countTotalNewsByClanId(@Param("clanId") Long clanId);

    // 检查用户是否已点赞
    int countUserLike(@Param("newsId") Long newsId, @Param("userId") Long userId);
    // 插入点赞记录
    int insertUserLike(@Param("newsId") Long newsId, @Param("userId") Long userId);
    // 增加动态点赞数
    int incrementLikeCount(@Param("newsId") Long newsId);
    
    // 获取动态点赞详情
    List<Map<String, Object>> getNewsLikes(@Param("newsIds") List<Long> newsIds);
    
    // 获取用户对指定动态的点赞状态
    List<Map<String, Object>> getUserLikes(@Param("userId") Long userId, @Param("newsIds") List<Long> newsIds);
    
    // 取消点赞
    int deleteUserLike(@Param("newsId") Long newsId, @Param("userId") Long userId);
    // 减少动态点赞数
    int decrementLikeCount(@Param("newsId") Long newsId);
} 